package com.ymatou.iapi.godmch.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.createBuyerInviteCodeBean;
import com.ymatou.iapi.godmch.parameter.createIndepMchBean;
import com.ymatou.iapi.godmch.parameter.editmchBean;
import com.ymatou.iapi.godmch.parameter.getInviteDistributorPicBean;
import com.ymatou.iapi.godmch.parameter.registerBean;
import com.ymatou.iapi.godmch.parameter.registerByCodeBean;
import com.ymatou.iapi.godmch.service.createBuyerInviteCodeCall;
import com.ymatou.iapi.godmch.service.createIndepMchCall;
import com.ymatou.iapi.godmch.service.getInviteDistributorPicCall;
import com.ymatou.iapi.godmch.service.registerByCodeCall;
import com.ymatou.iapi.godmch.service.registerCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.business.service.shenqi.SqDistributorService;
import com.ymttest.database.model.godmch.buyer_invite_code;
import com.ymttest.database.model.godmch.buyer_invite_codeExample;
import com.ymttest.database.sqlwapper.godmch.buyer_invite_codeWapper;
/**
*创建买家邀请码
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_createBuyerInviteCode{
	private static createBuyerInviteCodeBean bean;
	private static createBuyerInviteCodeCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建买家邀请码");
	}

	@Before
	public void caseUp() {
		bean=new createBuyerInviteCodeBean();
		call=new createBuyerInviteCodeCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
     private void check_verify() throws InterruptedException {
	
		buyer_invite_codeWapper codeWapper=new buyer_invite_codeWapper();
		buyer_invite_codeExample example=new buyer_invite_codeExample();
		example.createCriteria().andUserIdEqualTo(bean.getUserId());
		buyer_invite_code code=codeWapper.selectByExample(example).get(0);
		Logger.verifyEquals("200", call.getStatusCode1(), "验证status");
		Logger.verifyEquals("请求成功", call.getMessage(), "验证Message");
		Logger.verifyEquals(code.getInviteCode(), call.getResult().get("code").getAsString(), "验证code");
		Logger.verifyEquals(code.getUserType(), bean.getUserRole(), "验证userType");
		Logger.verifyEquals(code.getCodeStatus(), 0, "验证status");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createBuyerInviteCode_001() {
		Logger.start(true,"创建买家邀请码-商户");
		try {
 
			Integer mchId=GodmchManageCallService.CreateSellerMch();
			bean.setUserId(mchId);
			bean.setUserRole(1);
			call.setData(bean);
			call.callService();			
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createBuyerInviteCode_001_1() {
		Logger.start(true,"创建买家邀请码-商户");
		try {
 
			createIndepMchBean createIndepMchBean= GodmchManageCallService.InitCreateIndepMchBean();
			createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
			
			bean.setUserId(createIndepMchCall.getmchId());
			bean.setUserRole(1);
			call.setData(bean);
			call.callService();		
			
			check_verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createBuyerInviteCode_002() {
		Logger.start(true,"创建买家邀请码-客服");
		try {
			Integer mchId=GodmchManageCallService.CreateSellerMch();
	        UserCreateBean usercreateBean= DGUserCallService.initCsUsercreateBean();
	        Integer csId=DGUserCallService.UserCreateCall(usercreateBean);
	        GodmchManageCallService.CreateCSCall(mchId, csId, 1);
	        
			bean.setUserId(mchId);
			bean.setUserRole(2);
			call.setData(bean);
			call.callService();			
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createBuyerInviteCode_003() {
		Logger.start(true,"创建买家邀请码-分销员-公有云");
		try {
			
			registerBean registerBean=GodmchManageCallService.initRegister();
			registerCall registerCall=GodmchManageCallService.registerCall(registerBean);
            Integer mchId=registerCall.getUserId();
			editmchBean editBean=GodmchManageCallService.IniteditmachBean(mchId);
			GodmchManageCallService.editmachCall(editBean);
            Logger.comment("###########商户："+mchId);
            
            Logger.comment("###########商户审核：");
            SqDistributorService.createAutoAuditDistributor((long)mchId);
            
        	//独立商户邀请分享图
            getInviteDistributorPicBean getInviteDistributorPicBean=GodmchManageCallService.initGetInviteDistributorPicBean(mchId);
            getInviteDistributorPicCall getInviteDistributorPicCall=GodmchManageCallService.getInviteDistributorPicBeanCall(getInviteDistributorPicBean);
            Logger.comment("###########商户图片：");
           
            registerBean  bean2=GodmchManageCallService.initRegister();
            bean2.setInviteCode(getInviteDistributorPicCall.getResult().get("inviteCode").getAsString());
            registerCall=GodmchManageCallService.registerCall(bean2);
			Integer agentId=registerCall.getUserId();
            
			bean.setUserId(agentId);
			bean.setUserRole(3);
			call.setData(bean);
			call.callService();			
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_createBuyerInviteCode_004() {
		Logger.start(true,"创建买家邀请码-分销员-独立云");
		try {
			
			createIndepMchBean createIndepMchBean=GodmchManageCallService.InitCreateIndepMchBean();
            createIndepMchCall createIndepMchCall=GodmchManageCallService.CreateIndepMchCall(createIndepMchBean);
            Integer mchId=createIndepMchCall.getmchId();
            Logger.comment("###########独立商户："+mchId);
            
            Logger.comment("###########独立商户审核：");
            SqDistributorService.createAutoAuditDistributor((long)mchId);
            
        	//独立商户邀请分享图
            getInviteDistributorPicBean getInviteDistributorPicBean=GodmchManageCallService.initGetInviteDistributorPicBean(mchId);
            getInviteDistributorPicCall getInviteDistributorPicCall=GodmchManageCallService.getInviteDistributorPicBeanCall(getInviteDistributorPicBean);
            Logger.comment("###########独立商户图片：");
            
            
            registerByCodeBean registerByCodeBean=GodmchManageCallService.initRegisterByCode(mchId, getInviteDistributorPicCall.getResult().get("inviteCode").getAsString());
            registerByCodeCall registerByCodeCall= GodmchManageCallService.registerByCodeCall(registerByCodeBean);
            
			bean.setUserId(registerByCodeCall.getRegMchId());
			bean.setUserRole(3);
			call.setData(bean);
			call.callService();			
			
			check_verify();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_createBuyerInviteCode_005() {
		Logger.start(true,"创建买家邀请码-商户-跑100万");
		try {
 
			//Integer mchId=GodmchManageCallService.CreateSellerMch();
			for(int i=1;i<1000000;i++)
			{
				bean.setUserId(i);
				bean.setUserRole(1);
				call.setData(bean);
				call.callService();			
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}